/**
 * 
 */
package com.excilys.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

/**
 * POJO account
 * 
 * @author excilys dev : VP
 * @version 1.0
 * 
 */
@SuppressWarnings("serial")
@Entity
public class Account implements Serializable {

	@Id
	@GeneratedValue
	private long accountId;
	@Column(unique = true)
	private String accountName;
	private String accountType;
	@ManyToOne(optional = false)
	private User accountUser;
	private long accountAmount;

	@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
	private DateTime accountCreationDate;

	@OneToMany(mappedBy = "transactionAccount", fetch = FetchType.LAZY)
	private List<Transaction> accountListTransactions;

	@OneToMany(mappedBy = "transferAccount", fetch = FetchType.LAZY)
	private List<TransactionTransfer> accountListTransfers;

	// ------------------------------------------------------------
	// ------------------------------------------------------------
	// ------------------------------------------------------------

	public Account() {
		super();
	}

	public Account(String name, String type, User user, long amount, DateTime creationDate) {
		this.accountName = name;
		this.accountType = type;
		this.accountUser = user;
		this.accountAmount = amount;
		this.accountCreationDate = creationDate;
	}

	public String toString() {
		String res = "accountUser = " + accountUser.getUserFirstName() + " / accountId = " + accountId + " / accountAmount = " + accountAmount + " / accountName = " + accountName
				+ " / accountType = " + accountType;
		return res;
	}

	// ------------------------------------------------------------
	// ------------------------------------------------------------
	// ------------------------------------------------------------

	public long getAccountId() {
		return accountId;
	}

	public void setAccountId(long accountId) {
		this.accountId = accountId;
	}

	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public String getAccountType() {
		return accountType;
	}

	public void setAccountType(String accountType) {
		this.accountType = accountType;
	}

	public User getAccountUser() {
		return accountUser;
	}

	public void setAccountUser(User accountUser) {
		this.accountUser = accountUser;
	}

	public long getAccountAmount() {
		return accountAmount;
	}

	public void setAccountAmount(long accountAmount) {
		this.accountAmount = accountAmount;
	}

	public List<Transaction> getAccountListTransactions() {
		return accountListTransactions;
	}

	public void setAccountListTransactions(List<Transaction> accountListTransactions) {
		this.accountListTransactions = accountListTransactions;
	}

	public List<TransactionTransfer> getAccountListTransfers() {
		return accountListTransfers;
	}

	public void setAccountListTransfers(List<TransactionTransfer> accountListTransfers) {
		this.accountListTransfers = accountListTransfers;
	}

	public DateTime getAccountCreationDate() {
		return accountCreationDate;
	}

	public void setAccountCreationDate(DateTime accountCreationDate) {
		this.accountCreationDate = accountCreationDate;
	}

	@Override
	public int hashCode() {
		int result = 7;
		final int multiplier = 17;
		result = multiplier * result + (accountName == null ? 0 : accountName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (obj instanceof Account) {
			Account other = (Account) obj;

			if (accountName != other.accountName) {
				if (accountName == null || !accountName.equals(other.accountName))
					return false;
			}
			return true;
		}
		return false;
	}

}
